﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;

namespace Kooboo.Commerce
{
    public class EMail
    {
        const String Replacement_Format = "##{0}##";

        public String Subject { get; set; }

        public String Body { get; set; }

        public Boolean HtmlEmail { get; set; }

        public IEnumerable<String> Receivers { get; set; }

        public Boolean Send()
        {
            try
            {
                MailMessage message = new MailMessage();
                foreach (var item in Receivers)
                {
                    message.To.Add(item);
                }
                message.Subject = Subject;
                message.Body = Body;
                message.IsBodyHtml = HtmlEmail;
                SmtpClient smtpClient = new SmtpClient();
                smtpClient.Send(message);
                return true;
            }
            catch (Exception e)
            {
                //Log
                throw e;
            }
        }

        public Boolean Send(Dictionary<String, String> subjectArgs, Dictionary<String, String> bodyArgs)
        {
            if (subjectArgs != null && !String.IsNullOrEmpty(Subject))
            {
                foreach (var item in subjectArgs)
                {
                    Subject = Subject.Replace(String.Format(Replacement_Format, item.Key), item.Value);
                }
            }

            if (bodyArgs != null && !String.IsNullOrEmpty(Body))
            {
                foreach (var item in bodyArgs)
                {
                    Body = Body.Replace(String.Format(Replacement_Format, item.Key), item.Value);
                }
            }

            return Send();
        }
    }
}
